package com.ming.springcloud.student.controller;

import com.ming.springcloud.student.pojo.Person;
import com.ming.springcloud.student.pojo.StudentEntity;
import com.ming.springcloud.student.service.IStudentService;
import com.ming.springcloud.user.api.util.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;

    @RequestMapping(value = "/info", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Person findPerson() {
        Person person = new Person();

        person.setId(1);
        person.setAge(18);
        person.setName("服务A_atm");

        return person;
    }

    /**
     * 查询所有学生信息
     *
     * @return
     */
    @RequestMapping("/findAllStudents")
    public List<StudentEntity> findAllStudent() {
        List<StudentEntity> studentEntityList = new ArrayList<>();
        List<StudentEntity> students = this.studentService.findAll();
        for (StudentEntity student :students ) {
            student.setServiceName("我是Service A");
            studentEntityList.add(student);
        }
        return studentEntityList;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @param
     * @return
     */
    @RequestMapping("/findStudentsById/{id}")
    public StudentEntity  findDataGroupById(@PathVariable("id")Integer id) {
        StudentEntity studentEntity = new StudentEntity();
        Optional<StudentEntity> students = this.studentService.findUserById(id);

        List<StudentEntity> studentEntityList = new ArrayList<>();
        studentEntity=students.get();
        studentEntity.setServiceName("我是Service A");
        studentEntityList.add(studentEntity);

        return studentEntity;
    }

    /**
     * 更新服务器
     *
     * @param
     * @param
     * @return
     */
    //  @RequestMapping(value = "/editStudent" , method = )
    @PostMapping("/editStudent")
    public ResponseEntity editDataGroup(@RequestBody StudentEntity students) {
        students.setServiceName("我是Service A");
        this.studentService.edit(students);
        return ResponseEntity.success();
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */

    @RequestMapping("/delStudent/{id}")
    public ResponseEntity delDataGroup(@PathVariable("id")Integer id) {
        this.studentService.delete(id);
        return ResponseEntity.success();
    }

    /**
     * 添加学生
     * @param students
     * @param
     * @return
     */
    @RequestMapping("/addStudent")
    public ResponseEntity addStudent(@RequestBody StudentEntity students) {
        students.setServiceName("我是Service A");
        this.studentService.save(students);
        return ResponseEntity.success();
    }
}
